/*
 * parserfdeclare.c
 *
 *  Created on: 2013-5-19
 *      Author: xkaying
 */

#include "parser.h"
#include "lexer.h"
#include "parserfdeclare.h"

#include <assert.h>

Declare *ParserFDeclare(Lexer *lexer, char *name, int isfinal)
{
	assert(lexer);
	int state;
	state = 0;
	Declare *pdeclare;
	pdeclare = 0;
	pdeclare = DeclareInit(name, isfinal);
	while (ptoken) {
		switch (state)
		{
			case 0: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Less: {
						state = 1;
						ptoken = LexerGetToken(lexer);
						continue;
					}
						//乘法代表引用
					case TokenType_Mul: {
						state=15;
						DeclareAppendMul(pdeclare, ptoken->name);
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Variable: {
						state = 2;
						DeclareAppendName(pdeclare, ptoken->name);
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的变量定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser declare状态机错误.状态%d.\n", state);
				return 0;
			}
			case 1: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
						//表示模板参数读取结束,准备读取变量名字
					case TokenType_More: {
						state = 10;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Variable: {
						state = 11;
						DeclareAppendTemplate(pdeclare, ptoken->name);
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的变量定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser declare状态机错误.状态%d.\n", state);
				return 0;
			}
			case 10: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Mul: {
						state=15;
						DeclareAppendMul(pdeclare, ptoken->name);
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Variable: {
						state = 2;
						DeclareAppendName(pdeclare, ptoken->name);
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的变量定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser declare状态机错误.状态%d.\n", state);
				return 0;
			}
			case 11: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_More: {
						state = 10;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Comma: {
						state = 12;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的变量定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser declare状态机错误.状态%d.\n", state);
				return 0;
			}
			case 12: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_More: {
						state = 10;
						printf("第%d行:警告 '>' 前面多了个','.自动对其忽略.\n", ptoken->line);
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Variable: {
						state = 11;
						DeclareAppendTemplate(pdeclare, ptoken->name);
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的变量定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser declare状态机错误.状态%d.\n", state);
				return 0;
			}
			case 15: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Variable: {
						state = 2;
						DeclareAppendName(pdeclare, ptoken->name);
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的变量定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser declare状态机错误.状态%d.\n", state);
				return 0;
			}
			case 2: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Equl: {
						state = 4;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BracketStart: {
						state = 3;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Comma:
					case TokenType_BracketEnd: {
						return pdeclare;
					}
					default: {
						printf("第%d行:错误的变量定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser declare状态机错误.状态%d.\n", state);
				return 0;
			}
			case 3: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BracketEnd: {
						state = 30;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Variable:
					case TokenType_Bin:
					case TokenType_Oct:
					case TokenType_Hex:
					case TokenType_Float:
					case TokenType_Decimal:
					case TokenType_Char:
					case TokenType_String: {
						//input list 读取
						state = 31;
						DeclareAppendNewValue(pdeclare, ptoken->type,
								ptoken->name);
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的变量定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser declare状态机错误.状态%d.\n", state);
				return 0;
			}
			case 30: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Comma:
					case TokenType_End: {
						return pdeclare;
					}
					default: {
						printf("第%d行:错误的变量定义.函数参数必须以')'号结尾.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser declare状态机错误.状态%d.\n", state);
				return 0;
			}
			case 31: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Comma: {
						state = 32;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BracketEnd: {
						state = 30;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的变量定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser declare状态机错误.状态%d.\n", state);
				return 0;
			}
			case 32: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BracketEnd: {
						state = 30;
						printf("第%d行:警告 ')' 前面多了个','.自动对其忽略.\n", ptoken->line);
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Variable:
					case TokenType_Bin:
					case TokenType_Oct:
					case TokenType_Hex:
					case TokenType_Float:
					case TokenType_Decimal:
					case TokenType_Char:
					case TokenType_String: {
						//input list 读取
						state = 31;
						DeclareAppendValue(pdeclare, ptoken->type,
								ptoken->name);
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的变量定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser declare状态机错误.状态%d.\n", state);
				return 0;
			}
			case 4: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Variable:
					case TokenType_Bin:
					case TokenType_Oct:
					case TokenType_Hex:
					case TokenType_Float:
					case TokenType_Decimal:
					case TokenType_Char:
					case TokenType_String: {
						//input list 读取
						state = 40;
						DeclareAppendNewValue(pdeclare, ptoken->type,
								ptoken->name);
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BlockStart: {
						state = 41;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的函数定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser function状态机错误.状态%d.\n", state);
				return 0;
			}
			case 40: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Comma:
					case TokenType_End: {
						return pdeclare;
					}
					default: {
						printf("第%d行:错误的变量定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser declare状态机错误.状态%d.\n", state);
				return 0;
			}
			case 41: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BlockEnd: {
						ptoken = LexerGetToken(lexer);
						//查找;
						state = 40;
						continue;
					}
					case TokenType_Variable:
					case TokenType_Bin:
					case TokenType_Oct:
					case TokenType_Hex:
					case TokenType_Float:
					case TokenType_Decimal:
					case TokenType_Char:
					case TokenType_String: {
						//input list 读取
						state = 42;
						DeclareAppendNewValue(pdeclare, ptoken->type,
								ptoken->name);
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BracketStart: {
						state = 44;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的函数定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser function状态机错误.状态%d.\n", state);
				return 0;
			}
			case 42: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BlockEnd: {
						state = 40;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Comma: {
						state = 43;
						ptoken = LexerGetToken(lexer);
						return pdeclare;
					}
					default: {
						printf("第%d行:错误的变量定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser declare状态机错误.状态%d.\n", state);
				return 0;
			}
			case 43: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BlockEnd: {
						state = 40;
						printf("第%d行:警告 '}' 前面多了个','.自动对其忽略.\n", ptoken->line);
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Variable:
					case TokenType_Bin:
					case TokenType_Oct:
					case TokenType_Hex:
					case TokenType_Float:
					case TokenType_Decimal:
					case TokenType_Char:
					case TokenType_String: {
						//input list 读取
						state = 42;
						DeclareAppendNewValue(pdeclare, ptoken->type,
								ptoken->name);
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的变量定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser declare状态机错误.状态%d.\n", state);
				return 0;
			}
			case 44: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BracketEnd: {
						ptoken = LexerGetToken(lexer);
						//查找;
						state = 48;
						continue;
					}
					case TokenType_Variable:
					case TokenType_Bin:
					case TokenType_Oct:
					case TokenType_Hex:
					case TokenType_Float:
					case TokenType_Decimal:
					case TokenType_Char:
					case TokenType_String: {
						//input list 读取
						state = 45;
						DeclareAppendNewValue(pdeclare, ptoken->type,
								ptoken->name);
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的函数定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser function状态机错误.状态%d.\n", state);
				return 0;
			}
			case 45: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BracketEnd: {
						state = 48;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Comma: {
						state = 46;
						ptoken = LexerGetToken(lexer);
						return pdeclare;
					}
					default: {
						printf("第%d行:错误的变量定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser declare状态机错误.状态%d.\n", state);
				return 0;
			}
			case 46: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BracketEnd: {
						printf("第%d行:警告 ')' 前面多了个','.自动对其忽略.\n", ptoken->line);
						ptoken = LexerGetToken(lexer);
						//查找;
						state = 48;
						continue;
					}
					case TokenType_Variable:
					case TokenType_Bin:
					case TokenType_Oct:
					case TokenType_Hex:
					case TokenType_Float:
					case TokenType_Decimal:
					case TokenType_Char:
					case TokenType_String: {
						//input list 读取
						state = 45;
						DeclareAppendValue(pdeclare, ptoken->type,
								ptoken->name);
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的函数定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser function状态机错误.状态%d.\n", state);
				return 0;
			}
			case 48: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BlockEnd: {
						printf("第%d行:警告 '}' 前面多了个','.自动对其忽略.\n", ptoken->line);
						state = 40;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BracketStart: {
						state = 44;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的变量定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser declare状态机错误.状态%d.\n", state);
				return 0;
			}
			default: {
				printf("parser declare状态机错误.状态default.\n");
			}
		}
	}
	return 0;
}
